layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 项目信息管理
     */
    var Project = {
        tableId: "projectTable"
    };

    /**
     * 初始化表格的列
     */
    Project.initColumn = function () {
        return [[
            {type: 'checkbox',rowspan: 2},
            {field: 'id', hide: true, title: ''},
            {field: 'name', sort: true, title: '项目名称',rowspan: 2},
            {field: '', sort: true, title: '工时合计',align:'center',colspan: 2},
            {field: 'countMember', sort: true, title: '成员人数',rowspan: 2},
            {title: '发生费用',align:'center',colspan: 3},
            {field: 'memo', hide: true,sort: true, title: '备注',rowspan: 2},
            {align: 'center', toolbar: '#tableBar', title: '操作',rowspan: 2}
        ],
            [
                {field: 'workHour', sort: true, title: '小时合计',rowspan: 2,templet: function(d){
                        return '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">' + (d.workHour) +
                            '</a>';
                    }},
                {field: 'workHour', sort: true, title: '人月',rowspan: 2,templet: function(d){
                        return '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">' + (d.workHour/8/22).toFixed(1) +
                        '</a>';
                    }},
            {field: 'moneyTrafficExpense', sort: true, title: '交通费'},
            {field: 'moneyMeal', sort: true, title: '饭费'},
            {field: 'money', sort: true, title: '合计'}]];
    };

    /**
     * 点击查询按钮
     */
    Project.search = function () {
        var queryData = {};
        queryData['name'] = $("#condition").val();
        table.reload(Project.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Project.openAddDlg = function () {
        func.open({
            title: '添加项目信息',
            content: Feng.ctxPath + '/project/add',
            tableId: Project.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Project.openEditDlg = function (data) {
        func.open({
            title: '修改项目信息',
            content: Feng.ctxPath + '/project/edit?id=' + data.id,
            tableId: Project.tableId
        });
    };

    /**
    * 点击成员
    *
    * @param data 点击按钮时候的行数据
    */
    Project.openMemberDlg = function (data) {
        func.open({
            title: '项目成员信息',
            content: Feng.ctxPath + '/project/member/?projectId=' + data.id,
            tableId: Project.tableId
        });
    };

    /**
     * 点击成员工时
     *
     * @param data 点击按钮时候的行数据
     */
    Project.openWorkDlg = function (data) {
        func.open({
            title: '项目成员工时信息',
            content: Feng.ctxPath + '/project/member/work/?projectId=' + data.id,
            tableId: Project.tableId
        });
    };
    Project.openDetailDlg = function (data) {
        func.open({
            title: '<<'+data.name+'>>的项目成员工时信息',
            maxmin:true,
            content: Feng.ctxPath + '/project/workhourstatic?projectId=' + data.id,
            tableId: Project.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    Project.exportExcel = function () {
        var checkRows = table.checkStatus(Project.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Project.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/project/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Project.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Project.tableId,
        url: Feng.ctxPath + '/project/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Project.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Project.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Project.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Project.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Project.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Project.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Project.onDeleteItem(data);
        } else if (layEvent === 'member') {
            Project.openMemberDlg(data);
        } else if (layEvent === 'work') {
            Project.openWorkDlg(data);
        } else if (layEvent === 'detail') {
            Project.openDetailDlg(data);
        }
    });
});
